package rpcdriver

import (
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver"
	vo2 "gluenet/internal/glue-srvp/domain/vo"

	"github.com/spf13/viper"
)

type Manager interface {
	Register(req *vo2.AgentRegisterReq, resp *vo2.AgentRegisterRes) error

	UnRegister(guid string) error
}
type RpcDriver interface {
	Call(method string, params []byte) ([]byte, error)

	Push(topic string, data []byte) error

	Invoke(uri string, data interface{}) error

	Manager
}

func init() {
	depend.Provide(NewRPCConfig)
	depend.Provide(NewRpcDriver)
}

const (
	keyNatsConfig = driver.KeyDriverPerfix + `rpc`
)

type RpcConfig struct {
	NatsUrl string `mapstructure:"rpc_config"`
}

func NewRPCConfig(vp *viper.Viper) (*RpcConfig, error) {
	cfg := new(RpcConfig)
	if err := vp.UnmarshalKey(keyNatsConfig, cfg); err != nil {
		return nil, err
	}
	return cfg, nil
}

func NewRpcDriver(cfg *RpcConfig) (RpcDriver, error) {
	return NewDriver(cfg.NatsUrl)
}
